package com.guangfeng.yql.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.guangfeng.yql.comm.util.CopyUtils;
import com.guangfeng.yql.comm.util.WrapperUtil;
import com.guangfeng.yql.comm.bean.R;
import org.springframework.beans.BeanUtils;
import com.guangfeng.yql.security.exception.BusinessException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.guangfeng.yql.bean.vo.UserVo;
import com.guangfeng.yql.bean.po.UserPo;
import com.guangfeng.yql.bean.dto.UserDto;
import com.guangfeng.yql.service.IUserService;

import java.util.List;
import java.util.Optional;

import com.guangfeng.yql.comm.controller.BaseController;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author yql
 * @since 2022-09-20
 */
@Api(tags = {"用户表管理"}, value = "用户表管理")
@RestController
@RequestMapping("/yql/user")
public class UserController extends BaseController {
    @Autowired
    private IUserService service;
    /**
     * 获取 User 列表
     */
    @ApiOperation("获取 User 列表")
    @GetMapping("v1/list")
    public R<List<UserVo>>getList(UserDto userDto){
        startPage();
        List<UserPo> list = service.list(WrapperUtil.getWrapper(CopyUtils.copy(userDto,UserPo.class)));
        return page(CopyUtils.copyPage(list,UserVo.class));
    }

    /**
     * 获取 UserPo 详情
     */
    @ApiOperation("获取 UserPo 详情")
    @GetMapping("v1/{id}")
    public R<UserVo>getInfo(@PathVariable("id") Long id){
        Optional.ofNullable(id).orElseThrow(()->new BusinessException("id 不能为空 "));
        return ok(CopyUtils.copy(service.getById(id),UserVo.class));
    }

    /**
     * 删除 一个 UserPo
     */
    @ApiOperation("删除 一个 UserPo ")
    @DeleteMapping("v1/{id}")
    public R deleteById(@PathVariable("id") Long id){
        Optional.ofNullable(id).orElseThrow(()->new BusinessException("id 不能为空 "));
        return rest(service.removeById(id));
    }

    /**
     * 更新 一个 UserPo
     */
    @ApiOperation("更新 一个 UserPo ")
    @PutMapping("v1")
    public R updateOne(@Validated @RequestBody UserDto userDto){
        return rest(service.updateById(CopyUtils.copy(userDto,UserPo.class)));
    }
    /**
     * 添加 一个 UserPo
     */
    @ApiOperation("添加 一个 UserPo ")
    @PutMapping("v1")
    public R insertOne(@Validated @RequestBody UserDto userDto){
        return rest(service.save(CopyUtils.copy(userDto,UserPo.class)));
    }
}
